package com.gzw.scm.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SearchView;
import com.google.gson.reflect.TypeToken;
import com.gzw.scm.MyApplication;
import com.gzw.scm.R;
import com.gzw.scm.adapter.SelectAdapter;
import com.gzw.scm.base.BaseActivity;
import com.gzw.scm.bean.BMInfo;
import com.gzw.scm.bean.Constant;
import com.gzw.scm.utils.HttpUtils;
import com.gzw.scm.utils.JsonUtil;
import com.gzw.scm.utils.T;
import com.gzw.scm.utils.UrlUtils;
import com.gzw.scm.wuyong.FCZZXActivity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * Created by Administrator on 2017/4/25.
 */

public class SelectKhActivity extends BaseActivity {
    private Context mContext;
    private ListView listView;
    private Toolbar toolbar;
    private Intent intent;
    private SearchView searchView;
    private SelectAdapter selectAdapter;
    private List<BMInfo> ls_bm,ls_allbm;
    private String type;
    @Override
    protected void setContentView() {
        setContentView(R.layout.activity_seach);
    }

    @Override
    protected void initViews() {
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        searchView=(SearchView) findViewById(R.id.searchView);
        listView=(ListView) findViewById(R.id.listView);
    }

    @Override
    protected void initData() {
        mContext = this;
        intent=getIntent();
        type=intent.getStringExtra("type");

        if(type.equals("3")){
            toolbar.setTitle("选择供应商");
        }else{
            toolbar.setTitle("选择机构");
        }
        setSupportActionBar(toolbar);
        ls_bm = new ArrayList<>();
        ls_allbm =new ArrayList<>();
    }

    @Override
    protected void initEvents() {
        toolbar.setNavigationOnClickListener(v -> finish()
        );

        // 设置搜索文本监听
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            // 当点击搜索按钮时触发该方法
            @Override
            public boolean onQueryTextSubmit(String query) {
                return false;
            }

            // 当搜索内容改变时触发该方法
            @Override
            public boolean onQueryTextChange(String newText) {
                if (!TextUtils.isEmpty(newText)){
                    if(type.equals("0")){
                    ShowOrderTask showOrderTask = new ShowOrderTask();
                    showOrderTask.execute(newText);
                    }
//                    else if(type.equals("1")){
//                        GetTask getTask = new GetTask();
//                        getTask.execute(newText);
//                    }else if(type.equals("2")){
//                        GetTask getTask = new GetTask();
//                        getTask.execute(newText);
//                    }else if(type.equals("3")){
//                        GetTsupplierTask getTask = new GetTsupplierTask();
//                        getTask.execute(newText);
//                    }else if(type.equals("4")){
//                        GetWBTask getTask = new GetWBTask();
//                        getTask.execute(newText);
//                    }
                }else{
                    listView.clearTextFilter();
                }
                return false;
            }
        });


    }


    /**
     * 获取下级部门数据请求
     * 总部
     */
    public class ShowOrderTask extends AsyncTask<String, Void, String> {

        protected String doInBackground(String... params) {
//            String  url = "belongid=" + MyApplication.getUserInfo().getBelongId()+"&name="+params[0];
            Map<String, String> map = new HashMap<>();
            map.put("belongid", String.valueOf(MyApplication.getUserInfo().getBelongId()));
            map.put("name", params[0]);
            String json = JsonUtil.toJson(map);
            return HttpUtils.doPost(UrlUtils.getInstance().getNewJavaUrl("tools.do","getdepartment"), "json="+json);
        }

        @Override
        protected void onPostExecute(final String result) {

            if (result == null || result.length() == 0) {
//                T.showShort(mContext,"连接服务器异常");
                showAlertDialog("连接异常");
                return;
            }
            if(result.contains("系统异常")){
                showAlertDialog("网络异常");
                return;
            }

            ls_bm = JsonUtil.getPersons(result, new TypeToken<List<BMInfo>>() {
            }.getType());
            ls_allbm.clear();
            ls_allbm.addAll(ls_bm);
            if(ls_allbm.size()==0){
//                T.showShort(mContext,"没有查出相应部门!");
                showAlertDialog("未查出相应部门");
            }
            if (listView.getAdapter() == null) {
                selectAdapter = new SelectAdapter(ls_allbm,mContext);
                listView.setAdapter(selectAdapter);
            } else {
                selectAdapter.notifyDataSetChanged();
            }
                clickListView();
        }

    }

    /**
     * 获取下级部门数据请求
     * 分部
     */
    public class GetTask extends AsyncTask<String, Void, String> {

        protected String doInBackground(String... params) {
//            String  url = "belongid=" + MyApplication.getUserInfo().getBelongId()+"&name="+params[0];
            Map<String, String> map = new HashMap<>();
            map.put("belongid", String.valueOf(MyApplication.getUserInfo().getBelongId()));
            map.put("deptid", String.valueOf(MyApplication.getUserInfo().getDid()));
            map.put("name", params[0]);
            String json = JsonUtil.toJson(map);
            return HttpUtils.doPost(UrlUtils.getInstance().getNewJavaUrl("tools.do","getdept"),"json="+json);
        }

        @Override
        protected void onPostExecute(final String result) {

            if (result == null || result.length() == 0) {
//                T.showShort(mContext,"连接服务器异常");
                showAlertDialog("连接异常");
                return;
            }

            if(result.contains("系统异常")){
                showAlertDialog("网络异常");
                return;
            }
            ls_bm = JsonUtil.getPersons(result, new TypeToken<List<BMInfo>>() {
            }.getType());
            ls_allbm.clear();
            ls_allbm.addAll(ls_bm);
            if(ls_allbm.size()==0){
//                T.showShort(mContext,"没有查出相应部门!");
                showAlertDialog("未查出相应部门");

            }
                if (listView.getAdapter() == null) {
                    selectAdapter = new SelectAdapter(ls_allbm,mContext);
                    listView.setAdapter(selectAdapter);
                } else {
                    selectAdapter.notifyDataSetChanged();
                }
                clickListView();
        }

    }

    /**
     * 获取下级部门数据请求
     * 分部外部调出
     */
    public class GetWBTask extends AsyncTask<String, Void, String> {

        protected String doInBackground(String... params) {
            SharedPreferences sharedPreferences = getSharedPreferences(Constant.USERINFO_KEY, Context.MODE_PRIVATE);
            int did = sharedPreferences.getInt(Constant.DID_KEY,0);
            Map<String, String> map = new HashMap<>();
            map.put("belongid", String.valueOf(MyApplication.getUserInfo().getBelongId()));
            map.put("deptid", String.valueOf(MyApplication.getUserInfo().getDid()));
            map.put("name", params[0]);
            String json = JsonUtil.toJson(map);
            return HttpUtils.doPost(UrlUtils.getInstance().getNewJavaUrl("tools.do","getDid"), "json="+json);
        }

        @Override
        protected void onPostExecute(final String result) {

            if (result == null || result.length() == 0) {
                showAlertDialog("连接异常");
                return;
            }

            ls_bm = JsonUtil.getPersons(result, new TypeToken<List<BMInfo>>() {
            }.getType());
            ls_allbm.clear();
            ls_allbm.addAll(ls_bm);
            if(ls_allbm.size()==0){
//                T.showShort(mContext,"没有查出相应部门!");
                showAlertDialog("未查出相应部门");

            }
                if (listView.getAdapter() == null) {
                    selectAdapter = new SelectAdapter(ls_allbm,mContext);
                    listView.setAdapter(selectAdapter);
                } else {
                    selectAdapter.notifyDataSetChanged();
                }
                clickListView();
            }
    }

    /**
     * 获取下级部门数据请求
     * RDC
     */
    public class GetTsupplierTask extends AsyncTask<String, Void, String> {

        protected String doInBackground(String... params) {
            SharedPreferences sharedPreferences = getSharedPreferences(Constant.USERINFO_KEY, Context.MODE_PRIVATE);
            int did = sharedPreferences.getInt(Constant.DID_KEY,0);
            String  url = "did=" + did +"&name="+params[0];
            return HttpUtils.doPost(UrlUtils.getInstance().getNewJavaUrl("rdcpda.do","getTsupplier"),url);
        }

        @Override
        protected void onPostExecute(final String result) {

            if (result == null || result.length() == 0) {
                showAlertDialog("连接异常");
                return;
            }

            ls_bm = JsonUtil.getPersons(result, new TypeToken<List<BMInfo>>() {
            }.getType());
            ls_allbm.clear();
            ls_allbm.addAll(ls_bm);
            if(ls_allbm.size()==0){
//                T.showShort(mContext,"没有查出相应部门!");
                showAlertDialog("未查出相应部门");

            }
            if (listView.getAdapter() == null) {
                selectAdapter = new SelectAdapter(ls_allbm,mContext);
                listView.setAdapter(selectAdapter);
            } else {
                selectAdapter.notifyDataSetChanged();
            }
            clickListView();
        }
    }


    /**
     * Created by Wangzhiqiang on 2015-10-14
     * Listview点击事
     */
    private void clickListView() {
        listView.setOnItemClickListener((parent, view, position, id) ->  {

                BMInfo bmInfo = ls_bm.get(position);
                Intent mIntent = new Intent();

                if(type.equals("0")) {
                    mIntent.setClass(mContext, FPZZXActivity.class);
                    mIntent.putExtra("did", bmInfo.getId());
                    startActivityForResult(mIntent, 1);// 请求码
                }
//                else if(type.equals("1")){
//                    startActivityForResult(mIntent, 1);// 请求码
//                }else if(type.equals("2")){
//                    mIntent.setClass(mContext, FCZZXActivity.class);
//                    mIntent.putExtra("eid", bmInfo.getId());
//                    startActivityForResult(mIntent, 1);// 请求码
//                }else if(type.equals("3")){
//                    mIntent.setClass(mContext, CKActivity.class);
//                    mIntent.putExtra("eid", bmInfo.getId());
//                    startActivityForResult(mIntent, 1);// 请求码
//                }else if(type.equals("4")){
//                    mIntent.setClass(mContext, WBZZXActivity.class);
//                    mIntent.putExtra("eid", bmInfo.getId());
//                    mIntent.putExtra("type","1");
//                    startActivityForResult(mIntent, 1);// 请求码
//                }

        });

    }


}
